package com.zhilianbao.leyaogo.ui.fragment.me.myaccount;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bql.recyclerview.swipe.SwipeMenuAdapter;
import com.bql.recyclerview.swipe.SwipeMenuCreator;
import com.bql.utils.EventManager;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.http.api.AccountApi;
import com.zhilianbao.leyaogo.http.callback.LoadingViewCallback;
import com.zhilianbao.leyaogo.model.response.UserLoginInfo;
import com.zhilianbao.leyaogo.model.response.me.MyCardVoucherResponse;
import com.zhilianbao.leyaogo.ui.activity.me.moneyaccount.CardDetailActivity;
import com.zhilianbao.leyaogo.ui.adapter.me.CardVoucherAdapter;
import com.zhilianbao.leyaogo.ui.fragment.base.SwipeRefreshAndLoadFragment;
import com.zhilianbao.leyaogo.utils.Utils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Response;

/**
 * 卡券
 * Created by liu on 2016/11/3.
 */
public class CardVoucherFragment extends SwipeRefreshAndLoadFragment<MyCardVoucherResponse> {
    CardVoucherAdapter mCardVoucherAdapter;
    @BindView(R.id.tv_activate_card_voucher)
    TextView tvActivateCardVoucher;

    public static CardVoucherFragment getInstance() {
        CardVoucherFragment fragment = new CardVoucherFragment();
        return fragment;
    }

    @Override
    public void onRcvItemClick(RecyclerView.ViewHolder holder, int position) {
        Bundle bundle = new Bundle();
        bundle.putLong(IntentConfig.CARD_ID_DATA, mList.get(position).getCardItemId());
        Utils.startActivity(getActivity(), CardDetailActivity.class, bundle);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_card_voucher;
    }


    @Override
    public SwipeMenuAdapter getAdapter() {
        return mCardVoucherAdapter;
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mActivity);
    }

    @Override
    public SwipeMenuCreator getSwipeMenuCreator() {
        return null;
    }

    @Override
    public void onLeftSwipeMenuClick(int adapterPosition, int menuPosition) {

    }

    @Override
    public void onRightSwipeMenuClick(int adapterPosition, int menuPosition) {

    }


    @Override
    public void onRefresh() {
        loadDataList(1, true);
    }

    @Override
    public void loadDataList(int curPage, boolean isPullToRefresh) {
        mCurPage = curPage;
        UserLoginInfo userLoginInfo = Utils.getUserLoginInfo();
        long userId = userLoginInfo.getUserId();
        long supplierId = Utils.getSupplierId();
        AccountApi.getMyCardVoucherList(mActivity, userId,supplierId, 0,mCurPage,pageSize, new RefreshAndLoadCallback<List<MyCardVoucherResponse>>(isPullToRefresh) {
            @Override
            public void errorLeftOrEmptyBtnClick(View v) {
                loadDataList(1, false);
            }

            @Override
            public void onResultSuccess(List<MyCardVoucherResponse> myCardVoucherResponses, @Nullable Response response, LoadingViewCallback callback) {
                handleRefreshAndLoadListData(mCurPage,callback, myCardVoucherResponses);
            }

            @Override
            public Drawable emptyDrawable() {
                return getResources().getDrawable(R.drawable.img_coupons_for_empty);
            }

            @Override
            public String emptyContent() {
                return "主人,还没有卡券哦~快去激活吧";
            }
        });
    }

    @Override
    public void onLoadMore() {
        loadDataList(mCurPage, true);
    }

    @Override
    protected void initToolbarHere() {
        initToolbar(getString(R.string.card_voucher));
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        mCardVoucherAdapter = new CardVoucherAdapter(mActivity, mList);
    }

    @OnClick(R.id.tv_activate_card_voucher)
    public void onClick() {
        start(ActivateCardVouvherFragment.getInstance());
    }

    @Override
    public boolean registerEventBus() {
        return true;
    }

    @Override
    protected void onHandleEvent(EventManager eventManager) {
        super.onHandleEvent(eventManager);
        switch (eventManager.getEventCode()) {
            case LeEventCode.MSG_CODE_CARD_VOUCHER_ACTIVATE_SUCCESS://卡券激活成功
                loadDataList(1, true);
                break;
        }
    }
}
